from pymediainfo import MediaInfo

class media_info():
    '''
    根据pymediainfo.mediaInfo库，提取给定路径对应视频文件的重要信息以及所有信息
    '''
    def __init__(self, path):
        mediainfo = MediaInfo.parse(path)
        #通用信息
        self.general_data = mediainfo.general_tracks[0].to_data()
        #视频信息
        self.video_data = mediainfo.video_tracks[0].to_data()
        #音频信息
        self.audio_data = mediainfo.audio_tracks[0].to_data()

    def overall_film_info(self):
        '''
        mediainfo中包含的视频所有信息
        '''
        return({'general_track':self.general_data, 'video_track':self.video_data, 'audio_track':self.audio_data})

    def general_video_audio_info(self):
        '''
        部分重要的general_track, video_track, audio_track 信息的合并结果
        '''
        return({**self.general_movie_info(), **self.video_info(), **self.audio_info()})

    def general_movie_info(self):
        '''
        从track_type=general的通用数据中提取的部分重要信息
        '''
        #电影路径
        file_root = self.general_data['complete_name']
        #电影名称
        file_name = self.general_data['file_name']
        #文件大小
        file_size = '%.2f GiB'%(self.general_data['file_size']/1024**3)
        #时长--毫秒转换
        duration = self.general_data['duration']/1000
        duration = '%.2d:%.2d:%.2d'%(duration//3600, duration%3600//60, duration%3600%60)
        #总比特率
        overall_bit_rate = '%d kbps'%(self.general_data['overall_bit_rate']//1000)
        #格式
        film_format = self.general_data['format']
        #帧速率
        frame_rate = '%.2f 帧/秒'%(float(self.general_data['frame_rate']))
        #创建日期
        file_creation_date = self.general_data['file_creation_date']
        #最近修改日期
        file_last_modification_date =  self.general_data['file_last_modification_date']
        return({'电影路径':file_root, '电影名称':file_name, '文件大小':file_size, '时长':duration, '总比特率':overall_bit_rate, 
                '格式':film_format, '帧速率':frame_rate, '创建日期': file_creation_date, '最近修改日期':file_last_modification_date})


    def video_info(self):
        '''
        部分重要的视频信息
        '''
        #视频比特率
        try:
            bit_rate = '%d kbps'%(self.video_data['bit_rate']/1000)
        except Exception:
            bit_rate=''
        #帧宽高
        weight_heght = '%dX%d'%(int(self.video_data['sampled_width']), int(self.video_data['sampled_height']))
        #视频格式
        video_format = self.video_data['format']
        #位深度
        bit_depth = '%d Bit'%(self.video_data['bit_depth'])
        #视频流大小
        try:
            stream_size = '%.2f GiB'%(self.video_data['stream_size']/1024**3)
        except Exception:
            stream_size=''
        # #视频语言
        # language = video_data['language']
        return({'视频比特率':bit_rate, '帧宽高':weight_heght, '视频格式':video_format, '位深度':bit_depth, '视频流大小':stream_size})

    def audio_info(self):
        '''
        部分重要的音频信息
        '''
        #音频格式
        audio_format = self.audio_data['format']
        #音频比特率
        try:
            bit_rate = '%d kbps'%(self.audio_data['bit_rate']/1000)
        except Exception:
            bit_rate=''
        #音频通道数
        channel_s = self.audio_data['channel_s']
        #音频输出通道
        channel_layout = self.audio_data['channel_layout']
        #音频采样率
        sampling_rate = '%.1f kHz'%(self.audio_data['sampling_rate']/1000)
        #音频帧速率
        frame_rate = '%.2f FPS'%(float(self.audio_data['frame_rate']))
        #音频流大小
        try:
            stream_size = '%.2f MiB'%(self.audio_data['stream_size']/1024**2)
        except Exception:
            stream_size=''
        #音频语言
        try:
            language = self.audio_data['language']
        except Exception:
            language=''
        try:
            other_language = self.audio_data['other_language']
            if type(other_language)==list:
                other_language = '/'.join(other_language)
        except Exception:
            other_language=''
        try:
            language = language+'/'+other_language
        except Exception:
            language=''
        return({'音频格式':audio_format, '音频比特率':bit_rate, '音频通道数':channel_s, '音频输出通道':channel_layout, 
                '音频采样率':sampling_rate, '音频帧速率':frame_rate, '音频流大小':stream_size, '音频语言':language})
   
